﻿Public Class frm_prod
    Dim check As New Validar
    Dim deshabilitarGuardar As Boolean = False
    Dim accesoDatos As New AccesoDatos("Producto")

    Enum actualizar
        modificar
        ingresar
    End Enum

    Dim accion As actualizar



    Private Sub frm_prod_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'dgv_producto.RowsDefaultCellStyle.BackColor = Color.Silver
        'dgv_producto.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGray
        'dgv_producto.RowHeadersVisible = False


        cargarCombo(cmb_proveedor, accesoDatos.leerTabla("Proveedor"), "idProveedor", "nombre")
        cargarCombo(cmb_tipoProd, accesoDatos.leerTabla("TipoProd"), "idTipoProd", "nombre")
        cargarCombo(cmb_tipoTela, accesoDatos.leerTabla("TipoTela"), "idTipoTela", "nombre")
        cargarGrilla(accesoDatos.leerProducto())

        cmb_proveedor.SelectedIndex = -1
        cmb_tipoProd.SelectedIndex = -1
        cmb_tipoTela.SelectedIndex = -1

        txt_idProd.ReadOnly = True
        cmb_proveedor.Enabled = False
        cmb_tipoProd.Enabled = False
        cmb_tipoTela.Enabled = False
        txt_stockMin.Enabled = False
        txt_descrip.Enabled = False
        txt_precioCompra.Enabled = False
        txt_precioVenta.Enabled = False
        cmd_eliminar.Enabled = False
        cmd_nuevaTela.Enabled = False
        cmd_nuevo.Focus()
    End Sub

    Private Sub txt_precioCompra_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_precioCompra.Leave
        If check.validarNumero(txt_precioCompra.Text) = False Then
            lbl_errorPrecioCompra.Text = "*Ingrese monto"
            txt_precioCompra.Focus()
            cmd_guardar.Enabled = False
            cmd_buscar.Enabled = False
        Else
            lbl_errorPrecioCompra.Text = ""
            cmd_guardar.Enabled = True
            cmd_buscar.Enabled = True
        End If
    End Sub

    Private Sub txt_precioVenta_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_precioVenta.Leave
        If check.validarNumero(txt_precioVenta.Text) = False Then
            lbl_errorPrecioVenta.Text = "*Ingrese monto"
            txt_precioVenta.Focus()
            cmd_guardar.Enabled = False
            cmd_buscar.Enabled = False
        Else
            lbl_errorPrecioVenta.Text = ""
            cmd_guardar.Enabled = True
            cmd_buscar.Enabled = True
        End If
    End Sub

    Private Sub txt_stockMin_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_stockMin.Leave
        If check.validarNumero(txt_stockMin.Text) = False Then
            lbl_errorStockMin.Text = "*Ingrese monto"
            txt_stockMin.Focus()
            cmd_guardar.Enabled = False
            cmd_buscar.Enabled = False
        Else
            lbl_errorStockMin.Text = ""
            cmd_guardar.Enabled = True
            cmd_buscar.Enabled = True
        End If
    End Sub


    Private Sub chk_stockMin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles chk_stockMin.Click
        If chk_stockMin.Checked = False Then
            txt_stockMin.Enabled = False
        Else
            txt_stockMin.Enabled = True
            cmd_buscar.Enabled = True
        End If
    End Sub


    Private Sub cmd_guardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_guardar.Click
        If validarBlancos() = True Then
            If MessageBox.Show("¿Está seguro que desea guardar los cambios?", "Confirmación", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.Yes Then
                If accion = actualizar.ingresar Then
                    accesoDatos.insertar(True, generarStringValores())
                Else
                    accesoDatos.Modificar(True, generarStringValores(), generarRestriccionUpdate("idProducto", txt_idProd))
                End If
                cargarGrilla(accesoDatos.leerProducto())
            Else
                Exit Sub
            End If
        End If
    End Sub

    Private Function generarRestriccionUpdate(ByVal campo As String, ByRef valor As TextBox) As String
        Dim restriccion As String
        restriccion = campo & " = " & valor.Text
        Return restriccion
    End Function

    Private Function generarStringValores() As String
        Dim stockMin As String
        If chk_stockMin.Checked = True Then
            stockMin = txt_stockMin.Text
        Else
            stockMin = "0"
        End If

        Dim valores As String = "idProveedor = " & cmb_proveedor.SelectedValue
        valores &= ", idTipoProd = " & cmb_tipoProd.SelectedValue
        valores &= ", idTipoTela = " & cmb_tipoTela.SelectedValue
        valores &= ", stockMinimo = " & stockMin
        valores &= ", cantidad = " & 0
        valores &= ", descripcion = " & txt_descrip.Text
        valores &= ", txt_precioCosto = " & txt_precioCompra.Text
        valores &= ", precioVenta = " & txt_precioVenta.Text
        Return valores
    End Function

    Private Sub cmd_salir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_salir.Click
        Me.Close()
    End Sub

    Private Sub cmd_nuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_nuevo.Click
        cargarGrilla(accesoDatos.leerProducto())

        cmb_proveedor.Enabled = True
        cmb_tipoProd.Enabled = True
        cmb_tipoTela.Enabled = True
        txt_stockMin.Enabled = True
        txt_descrip.Enabled = True
        txt_precioCompra.Enabled = True
        txt_precioVenta.Enabled = True

        txt_idProd.Text = ""
        cmb_proveedor.SelectedIndex = -1
        cmb_tipoProd.SelectedIndex = -1
        cmb_tipoTela.SelectedIndex = -1
        txt_descrip.Text = ""
        txt_precioCompra.Text = ""
        txt_precioVenta.Text = ""
        chk_stockMin.Checked = False
        txt_stockMin.Text = ""
        txt_stockMin.Enabled = False
        cmb_proveedor.Focus()
        cmd_nuevaTela.Enabled = True

        accion = actualizar.ingresar
    End Sub

    Private Sub cmd_cancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_cancelar.Click
        txt_idProd.Text = ""
        cmb_proveedor.SelectedIndex = -1
        cmb_tipoProd.SelectedIndex = -1
        cmb_tipoTela.SelectedIndex = -1
        txt_descrip.Text = ""
        txt_precioCompra.Text = ""
        txt_precioVenta.Text = ""
        chk_stockMin.Checked = False
        txt_stockMin.Text = ""
        txt_stockMin.Enabled = False
        cargarGrilla(accesoDatos.leerProducto())
        cmb_proveedor.Focus()

    End Sub


    Private Sub cargarCombo(ByRef combo As ComboBox, ByRef tablaACargar As Data.DataTable, ByVal indice As String, ByVal campo As String)
        combo.DataSource = Nothing
        combo.DataSource = tablaACargar
        combo.ValueMember = indice
        combo.DisplayMember = campo
    End Sub

    Private Sub cargarGrilla(ByVal tablaACargar As Data.DataTable)
        dgv_producto.DataSource = tablaACargar
    End Sub


    Private Sub frm_prod_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        If MessageBox.Show("¿Está seguro que desea salir?", "Confirmación", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            e.Cancel = False
            frm_menu.Show()
        Else
            e.Cancel = True
        End If
    End Sub

    Private Function validarBlancos() As Boolean
        If check.validarComboBlanco(cmb_proveedor.SelectedIndex) = True Then
            MessageBox.Show("Seleccione un proveedor.", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Stop)
            cmb_proveedor.Focus()
            Return False
        ElseIf check.validarComboBlanco(cmb_tipoProd.SelectedIndex) = True Then
            MessageBox.Show("Seleccione un tipo de producto.", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Stop)
            cmb_tipoProd.Focus()
            Return False
        ElseIf check.validarComboBlanco(cmb_tipoTela.SelectedIndex) = True Then
            MessageBox.Show("seleccione un tipo de tela.", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Stop)
            cmb_tipoTela.Focus()
            Return False
        ElseIf check.validarBlanco(txt_descrip.Text) = True Then
            MessageBox.Show("Ingrese una descripcion.", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Stop)
            txt_descrip.Focus()
            Return False
        ElseIf check.validarBlanco(txt_precioCompra.Text) = True Then
            MessageBox.Show("Ingrese un precio de compra.", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Stop)
            txt_precioCompra.Focus()
            Return False
        ElseIf check.validarBlanco(txt_precioVenta.Text) = True Then
            MessageBox.Show("Ingrese un precio de venta.", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Stop)
            txt_precioVenta.Focus()
            Return False
        ElseIf chk_stockMin.Checked = True Then
            If check.validarBlanco(txt_stockMin.Text) = True Then
                MessageBox.Show("Ingrese un stock mínimo.", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Stop)
                txt_stockMin.Focus()
                Return False
            End If
        End If
        Return True
    End Function

    Private Sub dgv_producto_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgv_producto.DoubleClick

        cmb_proveedor.Enabled = True
        cmb_tipoProd.Enabled = True
        cmb_tipoTela.Enabled = True
        txt_stockMin.Enabled = True
        txt_descrip.Enabled = True
        txt_precioCompra.Enabled = True
        txt_precioVenta.Enabled = True

        accion = actualizar.modificar
        Dim id As String = dgv_producto.CurrentRow.Cells(0).Value
        Dim tabla As Data.DataTable = accesoDatos.obtenerProducto(id)

        txt_idProd.Text = tabla.Rows(0)("idProducto")
        cmb_proveedor.SelectedValue = tabla.Rows(0)("idProveedor")
        cmb_tipoProd.SelectedValue = tabla.Rows(0)("idTipoProd")
        cmb_tipoTela.SelectedValue = tabla.Rows(0)("idTipoTela")

        If IsDBNull(tabla.Rows(0)("stockMinimo")) Then
            txt_stockMin.Text = ""
        Else
            txt_stockMin.Text = tabla.Rows(0)("stockMinimo")
        End If

        txt_descrip.Text = tabla.Rows(0)("descripcion")
        txt_precioCompra.Text = tabla.Rows(0)("precioCosto")
        txt_precioVenta.Text = tabla.Rows(0)("precioVenta")

        txt_idProd.Enabled = False
        cmd_eliminar.Enabled = True

    End Sub

    Private Sub cmd_eliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_eliminar.Click

        If MessageBox.Show("¿Está seguro que desea eliminar?", "Atención", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = Windows.Forms.DialogResult.OK Then
            accesoDatos.eliminar("idProducto", txt_idProd.Text)
            cargarGrilla(accesoDatos.leerProducto())
            txt_idProd.Text = ""
            cmb_proveedor.SelectedIndex = -1
            cmb_tipoProd.SelectedIndex = -1
            cmb_tipoTela.SelectedIndex = -1
            txt_descrip.Text = ""
            txt_precioCompra.Text = ""
            txt_precioVenta.Text = ""
            chk_stockMin.Checked = False
            txt_stockMin.Text = ""
            txt_stockMin.Enabled = False
            cmb_proveedor.Focus()
        End If

    End Sub

    Private Sub cmd_buscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_buscar.Click
        frm_buquedaProducto.ShowDialog()
    End Sub

    Public Sub buscarPorId(ByVal id As String)
        dgv_producto.DataSource = accesoDatos.consulta("select prod.idProducto as 'ID', prov.nombre as 'Proveedor', tp.nombre as 'Tipo Producto', tt.nombre as 'Tela',  prod.descripcion as 'Descripcion' from Producto prod INNER JOIN Proveedor prov on(prod.idProveedor=prov.idProveedor) INNER JOIN TipoTela tt on (tt.idTipoTela=prod.idTipoTela) INNER JOIN TipoProd tp on (tp.idTipoProd=prod.idTipoProd) where idProducto = " & id)
    End Sub

    Public Sub cargarComboParaVolverDeTipoTela()
        cargarCombo(cmb_tipoTela, accesoDatos.leerTabla("TipoTela"), "idTipoTela", "nombre")
    End Sub

    Private Sub cmd_nuevaTela_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_nuevaTela.Click
        frm_tipoTela.ShowDialog()
    End Sub
End Class
